﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emEducation : emMasterDetailEdit
    {
        private data.baseDS.emEducationDataTable myDataTbl = new data.baseDS.emEducationDataTable();
        public emEducation()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception er)
            {
                ErrorHandler(this, er);;
            }            
        }
        public override event EventHandler myOnError
        {
            add
            {
                base.myOnError += value;
                emEducationDetail.myOnError += value;
            }
            remove
            {
                base.myOnError -= value;
                emEducationDetail.myOnError -= value;
            }
        }

        public override void Init()
        {
            base.Init();
            emEducationDetail.Init();
            SetData(myDataTbl);
        }
        public override void LoadData(string emCode) 
        {
            myDataTbl.Clear();
            if (emCode != null)
            {
                application.dataLibs.LoadData(myDataTbl, emCode);
            }
        }
        public override void SaveData(string emCode)
        {
            gridDataSource.EndEdit();
            for (int idx = 0; idx < myDataTbl.Count; idx++)
            {
                if (myDataTbl[idx].RowState == DataRowState.Deleted) continue;
                myDataTbl[idx].emCode = emCode;
            }
            application.dataLibs.UpdateData(myDataTbl);
        }
        public override void LockEdit(bool state)
        {
            base.LockEdit(state);
            emEducationDetail.LockEdit(state);
        }
        public override bool CheckData()
        {
            return emEducationDetail.CheckData();
        }
        public override void SetFocus()
        {
            emEducationDetail.SetFocus();
        }
        public override void AddNew()
        {
            data.baseDS.emEducationRow row;
            gridDataSource.AddNew();
            if (this.gridDataSource.Current != null)
            {
                row = (data.baseDS.emEducationRow)((DataRowView)this.gridDataSource.Current).Row;
                application.dataLibs.InitData(row);
            }
            emEducationDetail.SetDataSource(gridDataSource);
        }

        private void SetData(data.baseDS.emEducationDataTable tbl)
        {
            gridDataSource.DataSource = tbl;
            emEducationDetail.SetDataSource(gridDataSource); 
           
            DataGridViewTextBoxColumn schoolNameColumn = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn gradYearColumn = new DataGridViewTextBoxColumn();
            this.detailGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] 
                                        {
                                            schoolNameColumn, gradYearColumn
                                        }
                                         );

            // 
            // schoolNameColumn
            // 
            schoolNameColumn.DataPropertyName = tbl.schoolNameColumn.ColumnName;
            schoolNameColumn.HeaderText = "Trường";
            schoolNameColumn.Name = tbl.schoolNameColumn.ColumnName;
            schoolNameColumn.ReadOnly = true;
            // 
            // gradYearColumn
            // 
            gradYearColumn.DataPropertyName = tbl.gradYearColumn.ColumnName;
            gradYearColumn.HeaderText = "Năm TN";
            gradYearColumn.Name = tbl.gradYearColumn.ColumnName;
            gradYearColumn.ReadOnly = true;
            gradYearColumn.Width = 100;

            common.system.AutoFitGridColumn(detailGrid, schoolNameColumn.Name);
        }
    }
}
